﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TexasHoldem.RankedHands
{
    public class TwoPairHand: RankedHand
    {
        private TwoPairHand(Hand hand) : base(hand) { }
        public static TwoPairHand TryCreateFrom(Hand hand)
        {
            if (hand.Cards.GroupBy(c => c.Face).Where(g => g.Count() >= 2).Count() >= 2)
                return new TwoPairHand(hand);
            return null;
        }

        public override string RankName
        {
            get { return "Two Pair"; }
        }

        protected override ushort RankingStrength
        {
            get { return 2; }
        }

        protected override IEnumerable<Card> FindDefiningCards()
        {
            var groupsByFace = Cards.GroupBy(c => c.Face)
                     .OrderByDescending(g => g.Count())
                     .ThenByDescending(g => g.First().Value);
            return groupsByFace.Take(2).SelectMany(g => g);
        }
    }
}
